const app = getApp();
const {
  plupload,
  uploadImg,
  deleteImg,
  imgList,
  sendReadRequst,
  readOver
} = require('../../../config.js')

Page({

  data: {
    readshow: false,
    setTimeoutTip: true,
    imgTip1: false,
    imgTip2: false,
    btnTip: false,
    showImg1: false,
    imgUrlList1: [],
    imgUrlList2: [],
    imgSLList1: ['../../../image/mofen.jpg'],
    imgSLList2: ['../../../image/dyl.jpg'],
    showImg2: false,
    showItem1: false,
    showItem2: false,
    steps: [
      {
        text: '基础读取',
      },
      {
        text: 'A4读取',
      },
      {
        text: 'A3读取',
      },
      {
        text: '故障读取',
      }
    ],
    steps2: [
      {
        text: '基础读取',
      },
      {
        text: 'A4读取',
      },
      {
        text: '故障读取',
      }
    ],
    stepActive: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.getdata.A3){
      this.setData({ showA3: true })
    }else{
      this.setData({ showA3: false })
    }
    this.imgView1()
    this.imgView2()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.hideLoading()
    this.data.setTimeoutTip = false
  },
  getPhoto(){
    let that = this;
    wx.chooseImage({
      count:1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilesSize = res.tempFiles[0].size;  //获取图片的大小，单位B
        if (tempFilesSize <= 2000000) {

          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          console.log(res.tempFilePaths)

          if (tempFilePaths) {
            wx.uploadFile({
              url: plupload, 
              filePath: tempFilePaths[0],
              name: 'file',
              formData: {
                'user': 'test'
              },
              success(res) {
                console.log(111, JSON.parse(res.data).url)
                let imgurl = JSON.parse(res.data).url
                app.appRequest({
                  url: uploadImg,
                  data: {
                    id: app.globalData.getdata.id,
                    step: 1,
                    genre: 1,
                    address: imgurl
                  },
                  success: (res) => {
                    wx.showToast({
                      title: '墨量图片上传成功',
                      icon: 'none'
                    })
                    that.data.imgTip1 = true
                    that.imgView1()
                  }
                })
              }
            })
          }
        }else{
          wx.showToast({
            title: '上传图片不能大于2M', 
            icon: 'none'     
          })
        }

      }
    })

  },

  getPhoto2() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilesSize = res.tempFiles[0].size;  //获取图片的大小，单位B
        if (tempFilesSize <= 2000000) {

          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          console.log(res.tempFilePaths)

          if (tempFilePaths) {
            wx.uploadFile({
              url: plupload,
              filePath: tempFilePaths[0],
              name: 'file',
              formData: {
                'user': 'test'
              },
              success(res) {
                console.log(111, JSON.parse(res.data).url)
                let imgurl = JSON.parse(res.data).url
                app.appRequest({
                  url: uploadImg,
                  data: {
                    id: app.globalData.getdata.id,
                    step: 1,
                    genre: 2,
                    address: imgurl
                  },
                  success: (res) => {
                    wx.showToast({
                      title: '打印量图片上传成功',
                      icon: 'none'
                    })
                    that.data.imgTip2 = true
                    that.imgView2()
                  }
                })
              }
            })
          }
        } else {
          wx.showToast({
            title: '上传图片不能大于2M',
            icon: 'none'
          })
        }

      }
    })

  },

  imgView1(){
    app.appRequest({
      url: imgList,
      data: {
        id: app.globalData.getdata.id,
        step: 1,
        genre: 1
      },
      success: (res) => {
        let imgUrlList = [];
        res.data.forEach((item, index) => {
          imgUrlList[index] = item.address
        })
        if (imgUrlList.length > 0 && imgUrlList.length < 3) {
          let index = 3 - imgUrlList.length
          for (let i = 0; i < index; i++) {
            imgUrlList.push('')
          }
        }
        this.setData({
          imgUrlList1: imgUrlList
        })
      }
    })
  },
  imgView2(){
    app.appRequest({
      url: imgList,
      data: {
        id: app.globalData.getdata.id,
        step: 1,
        genre: 2
      },
      success: (res) => {
        let imgUrlList = [];
        res.data.forEach((item, index) => {
          imgUrlList[index] = item.address
        })
        if (imgUrlList.length > 0 && imgUrlList.length < 3) {
          let index = 3 - imgUrlList.length
          for (let i = 0; i < index; i++) {
            imgUrlList.push('')
          }
        }
        this.setData({
          imgUrlList2: imgUrlList
        })
      }
    })
  },
  delImg1(e){
    let imgurl = e.currentTarget.dataset.imgurl;
    app.appRequest({
      url: deleteImg,
      data: {
        id: app.globalData.getdata.id,
        step: 1,
        genre: 1,
        address: imgurl
      },
      success: (res) => {
        wx.showToast({
          title: '删除成功',
          icon: 'none'
        })
        this.imgView1()
      }
    })
  },
  delImg2(e){
    console.log(22)
    let imgurl = e.currentTarget.dataset.imgurl;
    app.appRequest({
      url: deleteImg,
      data: {
        id: app.globalData.getdata.id,
        step: 1,
        genre: 2,
        address: imgurl
      },
      success: (res) => {
        wx.showToast({
          title: '删除成功',
          icon: 'none'
        })
        this.imgView2()
      }
    })
  },
  showImg1(){
    this.setData({ showImg1: true });
  },
  showImg2(){
    this.setData({ showImg2: true });
  },
  onClose() {
    this.setData({ showImg1: false, showImg2: false });
  },
  closeItem1() {
    this.setData({ showItem1: false });
  },
  closeItem2() {
    this.setData({ showItem2: false });
  },
  getItem1(){
    this.setData({ showItem1: true });
  },
  getItem2(){
    this.setData({ showItem2: true });
  },
  beginRead(){
    if (this.data.setTimeoutTip) {
      console.log(this.data.imgTip1, this.data.imgTip2)
      if (this.data.imgTip1 && this.data.imgTip2){
        
      }else{
        wx.showToast({
          title: '请上传墨量信息图片和打印量图片！',
          icon: 'none',
        })
        return
      }
      wx.showLoading({
        title: '读取中...',
      })
      this.setData({ btnTip: true })
      // 开始读取
      app.appRequest({
        url: sendReadRequst,
        data: {
          id: app.globalData.getdata.id,
          step: 1
        },
        success: (res) => {
          if (res.code == 0) {
            if(res.data.err){
              wx.hideLoading()
              wx.showToast({
                title: '读取超时',
                icon: 'none',
              })
              this.setData({ btnTip: false })
            }else{
              if (res.data.bean.status == 2) {
                wx.hideLoading()
                wx.showToast({
                  title: '读取成功',
                })
                this.setData({ readshow: true, btnTip: true })
              } else {
                setTimeout(() => { this.beginRead() }, 2000)
              }
            }
          } else {
            wx.showToast({
              title: res.msg
            })
          }
        }
      })
    }
  },
  nextA4() {
    wx.navigateTo({
      url: '/pages/gatherData/getData3/getData3',
    })
  },
  quit() {
    app.appRequest({
      url: readOver,
      data: {
        id: app.globalData.getdata.id,
        status: 99
      },
      success: (res) => {
        if (res.code == 0) {
          app.globalData.getdata.id = '';
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }
    })
  }
 
})